package com.ruoyi.jdbc.mapper;

import com.ruoyi.jdbc.domain.Order;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单表 Mapper
 * 注意：SQL 始终写逻辑表 t_order
 *      ShardingSphere 会自动路由到物理表 t_order_yyyyMM
 * @author li yang
 */
@Mapper
public interface OrderMapper {

    // 插入订单
    @Insert(" INSERT INTO t_order (user_id, amount, status, create_time, update_time) " +
            "VALUES (#{userId}, #{amount}, #{status}, #{createTime}, #{updateTime})")
    int insert(Order o);

    // 按 ID 查询（如果没有分片键，可能会跨表扫描）
    @Select(" SELECT id, user_id, amount, status, create_time, update_time " +
            "FROM t_order " +
            "WHERE id = #{id} ")
    Order findById(Long id);

    // 按时间范围查询（推荐，能精确命中对应月表集合）
    @Select(" SELECT id, user_id, amount, status, create_time, update_time " +
            "FROM t_order " +
            "WHERE create_time BETWEEN #{start} AND #{end} " +
            "ORDER BY create_time DESC ")
    List<Order> queryByTime(@Param("start") LocalDateTime start,
                            @Param("end")   LocalDateTime end);
}

